package com.shop.controller;

import com.shop.domain.ContractProduct;
import com.shop.domain.ExtCproduct;
import com.shop.domain.Factory;
import com.shop.domain.SysCode;
import com.shop.service.ContractProductService;
import com.shop.service.ExtCproductService;
import com.shop.service.FactoryService;
import com.shop.service.SysCodeService;
import com.shop.util.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ContractProductController
 * Created by heqianqian on 2017/7/12.
 */
@Controller
@RequestMapping("/cargo/product")
public class ContractProductController extends BaseController {

    @Resource
    private ContractProductService<ContractProduct> contractProductService;

    @Resource
    private FactoryService<Factory> factoryService;

    @Resource
    private SysCodeService<SysCode> sysCodeSysCodeService;

    /**
     * 跳转到增加商品页面
     */
    @RequestMapping("/create")
    public String create(@RequestParam("contractId") Long contractId, Model model) {
        //添加合同id
        model.addAttribute("contractId", contractId);
        //添加下拉工厂信息
        List<Factory> factories = factoryService.listFactory();
        model.addAttribute("factoryList", factories);
        //添加货物列表
        Map<String, Object> map = new HashMap<>();
        map.put("contractId", contractId);
        List<ContractProduct> contractProducts = contractProductService.find(map);
        model.addAttribute("dataList", contractProducts);
        //添加分类信息
        List<SysCode> ctypeList = sysCodeSysCodeService.getCtypeList();
        model.addAttribute("ctypeList", ctypeList);
        //跳转到添加商品页面
        return "/cargo/product/contract_product_create";
    }

    /**
     * 增加商品
     */
    @RequestMapping("/insert")
    public String insert(ContractProduct contractProduct, Model model) {
        //保存商品
        contractProductService.insert(contractProduct);
        //添加合同id
        model.addAttribute("contractId", contractProduct.getContractId());
        //重定向到添加页面
        return "redirect:/cargo/product/create";
    }

    /**
     * 跳转到修改页面
     */
    @RequestMapping("/modify")
    public String modify(Long id, Model model) {
        //添加工厂下拉信息
        List<Factory> factories = factoryService.listFactory();
        model.addAttribute("factoryList", factories);
        //添加货物信息
        ContractProduct contractProduct = contractProductService.get(id);
        model.addAttribute("obj", contractProduct);
        //添加附件信息
        List<SysCode> ctypeList = sysCodeSysCodeService.getCtypeList();
        model.addAttribute("ctypeList", ctypeList);
        //重定向到更新页面
        return "cargo/product/contract_product_update";
    }

    /**
     * 修改货物信息
     */
    @RequestMapping("/update")
    public String update(ContractProduct contractProduct, Model model) {
        contractProductService.update(contractProduct);
        model.addAttribute("contractId", contractProduct.getContractId());
        return "redirect:/cargo/product/create";
    }


    /**
     * 删除货物信息
     */
    @RequestMapping("/delete")
    public String delete(Long contractId, Long id, Model model) {
        contractProductService.deleteById(id);
        model.addAttribute("contractId", contractId);
        return "redirect:/cargo/product/create";
    }

}
